package com.xrz.sxm.aj.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.ViewUtils;
import com.xrz.sxm.aj.activity.R;
import com.xrz.sxm.aj.utils.BaseDialog;
import com.xrz.sxm.aj.utils.BaseDialog.onDialogBtnClickListener;
import com.xrz.sxm.aj.utils.ExitUtil;
import com.xrz.sxm.aj.utils.ScreenUtil;

public abstract class BaseAct extends Activity {

	protected final String TAG = this.getClass().getSimpleName();

	protected TextView backBtn;
	protected TextView titleTv;
	protected TextView rightBtn;
	protected RelativeLayout topRela;
	protected int Topwidth, Topheight;

	protected Context ctx;

	private Bundle mBundle;

	// abstract mothed
	/**
	 * 设置ContentView
	 */
	protected abstract int setView();

	/**
	 * 功能：初始化信息
	 */
	protected abstract void init();

	/**
	 * 设置顶部菜单的标题
	 */
	protected abstract String setTopTitle();

	// private mothed
	/**
	 * 功能：在ContentView上加上topView
	 *
	 * @param resId
	 */
	private void createContentView(int resId) {
		RelativeLayout relativeLayout = new RelativeLayout(this);
		View toastLayout = LayoutInflater.from(ctx).inflate(
				R.layout.layout_notifitionview, null);
		LayoutInflater layoutInflater = LayoutInflater.from(this);
		LinearLayout layout = new LinearLayout(this);
		layout.setOrientation(LinearLayout.VERTICAL);
		View topView = layoutInflater.inflate(R.layout.top_base, null);
		int w = View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED);
		int h = View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED);
		topView.measure(w, h);
		Topwidth = topView.getMeasuredWidth();
		Topheight = topView.getMeasuredHeight();
		ScreenUtil.getInstant().putTopViewScreen(this, Topwidth, Topheight);
		initTopView(topView);
		View contentView = layoutInflater.inflate(resId, null);
		layout.addView(topView);
		LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);
		layout.addView(contentView, lp);
		android.widget.RelativeLayout.LayoutParams params1 = new android.widget.RelativeLayout.LayoutParams(
				android.widget.RelativeLayout.LayoutParams.MATCH_PARENT,
				android.widget.RelativeLayout.LayoutParams.MATCH_PARENT);
		relativeLayout.addView(layout, params1);
		android.widget.RelativeLayout.LayoutParams params = new android.widget.RelativeLayout.LayoutParams(
				android.widget.RelativeLayout.LayoutParams.MATCH_PARENT,
				android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
		params.topMargin = 60;
		relativeLayout.addView(toastLayout, params);
		setContentView(relativeLayout);
		if (topHide()) {
			topView.setVisibility(View.GONE);
		}
	}

	// 初始化顶部菜单
	private void initTopView(View topView) {
		backBtn = (TextView) topView.findViewById(R.id.top_back_img);
		titleTv = (TextView) topView.findViewById(R.id.top_tv);
		rightBtn = (TextView) topView.findViewById(R.id.top_right_btn_);
		topRela = (RelativeLayout) topView.findViewById(R.id.top_base_toprela);
	}

	private void initConfig() {
		getWindow().setSoftInputMode(// 设置不自动弹出软键盘
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mBundle = savedInstanceState;
		this.ctx = this;
		initConfig();
		createContentView(setView());
		ViewUtils.inject(this);
		ExitUtil.getInstant().addToPool(this);
		if (!topHide()) {
			if (setTopTitle() == null) {
				titleTv.setText("");
			} else {
				titleTv.setText(setTopTitle());
			}
			if (isBackBtnClickFinish())
				backBtn.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						finish();
					}
				});
		}
		init();
	}

	public Bundle GetBundle() {
		return mBundle;
	}

	/**
	 * 设置返回按钮是否点击之后是finish()
	 *
	 * @return
	 */
	protected boolean isBackBtnClickFinish() {
		return true;
	}

	/**
	 * 功能：设置顶部菜单是否隐藏
	 *
	 * @return
	 */
	protected boolean topHide() {
		return false;
	}

	/****************************************** Xutils图片加载相关 *************************************************/

	// protected final void loadImg(ImageView v, String url,
	// BitmapLoadCallBack<View> mCallBack) {
	// XUtilBitmapUtil.getInstant().display(this, v, url, mCallBack);
	// }

	/****************************************** Xutils图片加载相关 end *************************************************/

	/******************* Toast *********************/
	protected final void toastShort(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}

	protected final void toastLong(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
	}

	/******************* Toast end *********************/

	/**
	 * 显示一个提示对话框，只有一行文字和一个按钮
	 */
	protected final void showMessageDialog(String title, String text) {
		BaseDialog.showDialog(this, title, text, true, "确定", "", null, null);
	}

	/**
	 * 显示两个按钮的对话框
	 *
	 * @param text
	 *            正文文字
	 * @param okText
	 *            左边按钮文字
	 * @param cancleText
	 *            右边按钮文字
	 * @return
	 */
	protected final void showAskDialog(String text, String okText,
									   String cancleText, onDialogBtnClickListener listener1,
									   onDialogBtnClickListener listener2) {
		BaseDialog.showDialog(this, "系统提示", text, false, okText, cancleText,
				listener1, listener2);
	}
}
